package com.yanqu.road.server.cmd.activity.yqhotel;


import com.yanqu.road.entity.activity.yqhotel.result.YqHotelQuickStudentResult;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.YqHotelProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.yqhotel.YqHotelModule;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;


@Cmd(code = GameProtocol.S_YQ_HOTEL_QUICK_GRADUATE, desc = "阳泉酒家学生快速毕业")
public class YqHotelQuickGraduteCmd extends AbstractCommand {


    private int activityId;
    private int studentId;

    private YqHotelQuickStudentResult result;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        YqHotelProto.YqHotelQuickGraduateReqMsg reqMsg = YqHotelProto.YqHotelQuickGraduateReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        studentId = reqMsg.getStudentId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        Object locker = player.getModule(YqHotelModule.class).getLocker();
        synchronized (locker){
            result = player.getModule(YqHotelModule.class).quickGraduate(System.currentTimeMillis(),activityId,studentId);
        }
    }

    @Override
    public void sendData(GamePlayer player) {
        YqHotelProto.YqHotelQuickGraduateRespMsg.Builder respMsg = YqHotelProto.YqHotelQuickGraduateRespMsg.newBuilder();
        respMsg.setRet(result.getRet());
        if(result.getRet() == 0){
            respMsg.setStudent(player.getModule(YqHotelModule.class).parseStudentPb(result.getStudent()));
        }
        player.sendPacket(ClientProtocol.U_YQ_HOTEL_QUICK_GRADUATE, respMsg);
        player.getModule(YqHotelModule.class).syncStudent(result.getChangeStudent(),1);
    }
}
